package com.gzhryc.shared_device.oem.code.agent.services;

import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.oem.code.agent.dao.AgentFuncModelDao;
import com.gzhryc.shared_device.oem.code.agent.dao.db.AgentFuncModel;
import com.gzhryc.shared_device.oem.code.agent.dao.models.FuncModelCount;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class AgentFuncModelService {

    static Logger log = Logger.getLogger(AgentFuncModelService.class);

    public static AgentFuncModelService self() {
        return MultiDBTools.getService(AgentFuncModelService.class, true);
    }

    public static AgentFuncModelService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, AgentFuncModelService.class, true);
    }

    AgentFuncModelDao dao;

    public AgentFuncModelService(String jdbcKey) {
        this.dao = new AgentFuncModelDao(jdbcKey);
    }

    public List<AgentFuncModel> findJoinByUserId(Long userId){
        if(userId != null) {
            Conditions conditions = new Conditions(AgentFuncModel.class);
            conditions.and().is("userId",userId);
            try {
                return dao.findJoinByCondition(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return null;
    }

    public AgentFuncModel getJoinByKeys(Long userId, Long funcModelId){
        if(userId != null && funcModelId != null) {
            Conditions conditions = new Conditions(AgentFuncModel.class);
            conditions.and().is("userId",userId);
            conditions.and().is("funcModelId",funcModelId);
            try {
                return dao.getJoin(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return null;
    }

    public List<FuncModelCount> findCountByFuncModelIds(List<Long> funcModelIds){
        Conditions conditions = new Conditions(AgentFuncModel.class);
        conditions.and().in("funcModelId",funcModelIds);
        try {
            return dao.countGroupFuncModelId(conditions);
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

    public boolean updateState(Long userId,Long funcModelId,Integer state){
        if(userId != null && funcModelId != null && state != null) {
            Conditions conditions = new Conditions(AgentFuncModel.class);
            conditions.and().is("userId", userId);
            conditions.and().is("funcModelId", funcModelId);

            AgentFuncModel updateEntity = new AgentFuncModel();
            updateEntity.setState(state);

            try {
                return dao.updateNotNull(updateEntity, conditions) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return false;
    }

    public boolean add(AgentFuncModel userFuncModel) throws LogicException {
        userFuncModel.setCreateDate(new Date());
        try {
            return dao.insert(userFuncModel) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }

    public boolean save(AgentFuncModel agentFuncModel) throws LogicException {
        AgentFuncModel temp = getJoinByKeys(agentFuncModel.getUserId(),agentFuncModel.getFuncModelId());
        if(temp != null) {
            return true;
        }else{
            agentFuncModel.setCreateDate(new Date());
            try {
                return dao.insert(agentFuncModel) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return false;
    }

    public boolean deleteByKeys(Long userId,Long funcModelId) throws LogicException{
        if(userId != null && funcModelId != null) {
            Conditions conditions = new Conditions(AgentFuncModel.class);
            conditions.and().is("userId", userId);
            conditions.and().is("funcModelId", funcModelId);
            try {
                return dao.deleteByCondition(conditions) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return false;
    }

    public boolean deleteByFuncModelId(Long funcModelId){
        if(funcModelId != null) {
            Conditions conditions = new Conditions(AgentFuncModel.class);
            conditions.and().is("funcModelId", funcModelId);
            try {
                return dao.deleteByCondition(conditions) >= 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return false;
    }
}
